<?php
class FranjasModel extends CustomModel
{

    public function nuevaFranja($franja)
    {
        $franja['dias'] = 0;
        foreach ($franja['dias_bitmap'] as $dia)
        {
            $franja['dias'] = $franja['dias'] | $dia;
        }
        unset($franja['dias_bitmap']);
        $franjaAbm = new ABM('franjas');
        return $franjaAbm->insert($franja);
    }

    public function getFranjas($pageNumber)
    {
        $franjaAbm = new ABM('franjas');
        return $franjaAbm->getPaginated($pageNumber, array('status' => STATUS_ONLINE), 10, 'nombre ASC', true);
    }

    public function franjaExists($idFranja)
    {
        $franjaAbm = new ABM('franjas');
        $franja = $franjaAbm->get(array('idfranja' => $idFranja, 'status' => STATUS_ONLINE));
        if ($franja)
        {
            return $franja[$idFranja];
        }
        else
        {
            return false;
        }
    }

    public function borrarFranja($idFranja)
    {
        $franjaAbm = new ABM('franjas');
        return $franjaAbm->update(array('idfranja' => $idFranja), array('status' => STATUS_OFFLINE));
    }

    public function editarFranja($idFranja, $franja)
    {
        $franjaAbm = new ABM('franjas');
        $franja['dias'] = 0;
        foreach ($franja['dias_bitmap'] as $dia)
        {
            $franja['dias'] = $franja['dias'] | $dia;
        }
        unset($franja['dias_bitmap']);
        return $franjaAbm->update(array('idfranja' => $idFranja), $franja);
    }

    public function franjaAvailable($dias, $desde, $hasta, $id = false)
    {
        $filter = false;
        foreach ($dias as $dia)
        {
            if (!$filter) $filter = new dbFilter('dias', '&', $dia);
            else
            {
                $auxFilter = new dbFilter('dias', '&', $dia);
                $filter = new dbFilter($filter, 'OR', $auxFilter);
            }
        }
        $auxFilter = new dbFilter('status', '=', STATUS_ONLINE);
        $filter = new dbFilter($filter, 'AND', $auxFilter);
        $franjaAbm = new ABM('franjas');
        $sameDays  = $franjaAbm->get($filter);
        if (!empty($sameDays))
        {
            if (($id) && (isset($sameDays[$id]))) unset($sameDays[$id]);
            if (!empty($sameDays))
            {
                $timeOk = true;
                foreach ($sameDays as $franja)
                {
                    if (($desde < $franja['horario_desde'] && ($hasta < $franja['horario_desde'])) || ($desde > $franja['horario_hasta'] && ($hasta > $franja['horario_hasta'])))
                    {
                        // foo
                    }
                    else
                    {
                        $timeOk = false;
                    }
                }
                return $timeOk;
            }
            else
            {
                return true;
            }
        }
        else
        {
            return true;
        }
    }

}
?>